package com.young.mylib.utils;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;

import com.alibaba.fastjson.JSONArray;
import com.young.mylib.R;


import java.util.List;

/**
 * Created by Young on 2018/1/23.
 */

public class DialogUtils {

    public static AlertDialog buildSingleChoiceDialog(Context context, int icon, String title, String[] singleChoiceItems, DialogInterface.OnClickListener onSelectListener, DialogInterface.OnClickListener OkListener) {

        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setIcon(icon);
        builder.setTitle(title);
        builder.setPositiveButton("确定", OkListener);

        builder.setSingleChoiceItems(singleChoiceItems, 0, onSelectListener);

        builder.setCancelable(true);
        AlertDialog dialog = builder.create();
        return dialog;
    }

    public static AlertDialog buildListDialog(Context context,
                                              int icon,
                                              String title,
                                              List<String> listItems,
                                              String okStr,
                                              DialogInterface.OnClickListener positiveListener,
                                              String cancelStr,
                                              DialogInterface.OnClickListener negativeListener) {

        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setIcon(icon);
        builder.setTitle(title);

        builder.setPositiveButton(okStr, positiveListener);
        builder.setNegativeButton(cancelStr, negativeListener);

        final String[] items = new String[listItems.size()];
        listItems.toArray(items);
        builder.setItems(items, null);

        builder.setCancelable(true);
        AlertDialog dialog = builder.create();
        return dialog;
    }

    public static void listDialog(Context context,
                                  String title,
                                  JSONArray array,
                                  String okStr,
                                  DialogInterface.OnClickListener onOkListener,
                                  DialogInterface.OnClickListener onCanceListener) {
        if (array == null) return;
        StringBuilder sb = new StringBuilder();
        for (Object o : array) {
            sb.append(o.toString() + "\r\n");
        }

        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setIcon(R.mipmap.ic_app);
        builder.setMessage(sb.toString());
        builder.setTitle(title);
        builder.setPositiveButton(okStr,
                onOkListener);
        builder.setNegativeButton("cance", onCanceListener);
        builder.setCancelable(true);
        AlertDialog dialog = builder.create();
        dialog.show();
    }
}
